#!/usr/bin/python

import logging

class Playlist(object):
    
    def __init__(self, pl, a, t):
        self.trk_list = pl
        self.art = a
        self.tit = t

    def gen_m3u(self):
        '''
            m3u format
            ---
            #EXTM3U
            #EXTINF:233,Artist - Song
            directory\file_name.mp3.mp3
            #EXTINF:-1,My Cool Stream
            http://www.site.com:8000/listen.pls
            ---

            Taken from http://forums.winamp.com/showthread.php?s=&threadid=65772
        '''
        m3u =  "#EXTM3U\n"
        for trk in self.trk_list:
            m3u += "#EXTINF:-1,%s\n%s\n" % (trk[1], trk[0]) 

        return m3u

    def wrt_m3u(self, dir=None):
        '''
            write the newly created playlist to disk
        '''
        if dir == None:
            dir = "./"
        else:
            dir = "%s/" % dir

        f = "%s00-%s-%s.m3u" % (dir, self.art, self.tit)

        fh = open(f,"w")
        fh.write(self.gen_m3u().encode("utf-8"))
        fh.close()
    
        print f
    
    def prn_m3u(self):
        '''
            debug function to output complete playlist   
        '''
        print self.gen_m3u() 

def tag_logger(level = logging.WARNING):
    log = logging.getlogger()
    
    for l in log.handlers:
        log.removeHander(l)

    if len(logging.getLogger().handers) > 0:
        return

    formatter = logging.Formatter('%(asctime)s [%(levelname)s] %(module)s(%(lineno)s): %(message)s')

    handler = logging.StreamHandler()
    handler.setFormatter(formatter)
    log.addHandler(handler)

if __name__ == "__main__":

    p = Playlist()
    p.add_track(["file_one.mp3","File - One"])
    print p.prn_m3u()
